<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Guopan
 *
 * @author dragon.hu add @2016.2.17
 */
require_once 'BaseChannel.php';

//xx助手
class Guopan implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="ZAP7MHVFSSKTRJS743VY3SHFWCPD80VAHK9J6IPED6OUCCCWR7UMYLE6A1YA0VZE"; //server secret key 
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey="0ZE0SWLG2RZUGZ4CDFHWF98PQVY52I6TGP3FW6FLJNXGD6H7C7PJIHX6WZIT45Z2"; //server secret key
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("guopan", $_REQUEST);
		/*trade_no
		serialNumber
		money
		status
		t
		sign
		appid
		item_id
		item_price
		item_count
		reserved
		*/
		$trade_no = $_REQUEST['trade_no'];  //果盘唯一订单号
		$serialNumber = $_REQUEST['serialNumber'];  //游戏方订单序列号
		$money = $_REQUEST['money'];  //消费金额(单位元)
		$status = $_REQUEST['status'];  //状态(0失败，1成功，2余额不足失败)
		$t = $_REQUEST['t'];  //时间戳
		$sign = $_REQUEST['sign'];  //加密串
		$appid = $_REQUEST['appid'];  //
		$item_id = $_REQUEST['item_id'];  //
		$item_price = $_REQUEST['item_price'];  //
		$item_count = $_REQUEST['item_count'];  //
		$reserved = $_REQUEST['reserved'];  //扩展参数
		
		//加密串 sign=md5(serialNumber +money+status+t+SERVER_KEY) 是五个变量值拼接后经md5后的值，其中SERVER_KEY在果盘开放平台上获得
		$mysign = $serialNumber.$money.$status.$t.$paymentKey;
		
		
		if($sign == md5($mysign))  
		{ //验证通过,进行发货
			$tmpstatus = STATUS_SUCCESS;
			if (1 != $status)  //0=失败；1=成功；2=失败，原因是余额不足。
			{
				$tmpstatus = STATUS_FAILD;
			}
			
			if ($platform == ANDROID)
			{
				if(false == $orderLog->recordOrder($tmpstatus, $money, $serialNumber, "guopan", $platform,$trade_no))
				{
					$ret="failure";
					$orderLog->log("guopan",array("platform"=>$platform,"order"=>$serialNumber,"money"=>$money,"serverresult"=>"faild"));
				}
				else
				{
					//支付成功
					$ret="success";
					$orderLog->log("guopan",array("platform"=>$platform,"order"=>$serialNumber,"money"=>$money,"serverresult"=>"success"));
				}
			}
			else if($platform == IOS)
			{
				if(false == $orderLog->recordOrder($tmpstatus, $money, $serialNumber, "IosXX", $platform,$trade_no))
				{
					$ret="failure";
					$orderLog->log("guopan",array("platform"=>$platform,"order"=>$serialNumber,"money"=>$money,"serverresult"=>"faild"));
				}
				else
				{
					//支付成功
					$ret="success";
					$orderLog->log("guopan",array("platform"=>$platform,"order"=>$serialNumber,"money"=>$money,"serverresult"=>"success"));
				}
			}
			 
		} 
		else 
		{
			$orderLog->log("guopan"," platform:$platform order:$serialNumber  sign error sign:$sign mysign:$mysign");
			$ret="failure";
		}
		return $ret;
	}
	
}